Added asynchronous event notification through the Xen-API. Added a new command,
authorEwan Mellor <ewan@xensource.com>
Thu, 29 Mar 2007 14:46:30 +0000 (15:46 +0100)
committerEwan Mellor <ewan@xensource.com>
Thu, 29 Mar 2007 14:46:30 +0000 (15:46 +0100)
xm event-monitor, a new test program for the C bindings, and new bindings
themselves.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
12 files changed:
.hgignore
docs/xen-api/xenapi-datamodel.tex
tools/libxen/Makefile
tools/libxen/include/xen_event.h [new file with mode: 0644]
tools/libxen/include/xen_event_decl.h [new file with mode: 0644]
tools/libxen/include/xen_event_operation.h [new file with mode: 0644]
tools/libxen/include/xen_event_operation_internal.h [new file with mode: 0644]
tools/libxen/src/xen_event.c [new file with mode: 0644]
tools/libxen/src/xen_event_operation.c [new file with mode: 0644]
tools/libxen/test/test_event_handling.c [new file with mode: 0644]
tools/python/xen/xend/XendAPI.py
tools/python/xen/xm/main.py

index aa9b93a8570073e8e9d71a74d520b7daa5e6d63d..161f740eafa2124fe1049b4c62687132cd841ef6 100644 (file)
--- a/.hgignore
+++ b/.hgignore
 ^tools/ioemu/qemu\.pod$
 ^tools/libxc/xen/.*$
 ^tools/libxen/test/test_bindings$
+^tools/libxen/test/test_event_handling$
 ^tools/libaio/src/.*\.ol$
 ^tools/libaio/src/.*\.os$
 ^tools/misc/cpuperf/cpuperf-perfcntr$
index 827ccc4e578e5523592cd5f17b4f2216ddee7f4a..480761eff5ca738e4f79a897509efa23a98a7daa 100644 (file)
@@ -24,6 +24,7 @@ Name & Description \\
 \hline
 {\tt session} & A session \\
 {\tt task} & A long-running asynchronous task \\
+{\tt event} & Asynchronous event registration and handling \\
 {\tt VM} & A virtual machine (or 'guest') \\
 {\tt VM\_metrics} & The metrics associated with a VM \\
 {\tt VM\_guest\_metrics} & The metrics reported by the guest (as opposed to inferred from outside) \\
@@ -110,6 +111,17 @@ Map (a $\rightarrow$ b) & a table mapping values of type a to values of type b \
 \subsection{Enumeration types}
 The following enumeration types are used:
 
+\begin{longtable}{|ll|}
+\hline
+{\tt enum event\_operation} & \\
+\hline
+\hspace{0.5cm}{\tt add} & An object has been created \\
+\hspace{0.5cm}{\tt del} & An object has been deleted \\
+\hspace{0.5cm}{\tt mod} & An object has been modified \\
+\hline
+\end{longtable}
+
+\vspace{1cm}
 \begin{longtable}{|ll|}
 \hline
 {\tt enum console\_protocol} & \\
@@ -1014,6 +1026,114 @@ references to objects with match names
 \vspace{0.3cm}
 \vspace{0.3cm}
 
+\vspace{1cm}
+\newpage
+\section{Class: event}
+\subsection{Fields for class: event}
+\begin{longtable}{|lllp{0.38\textwidth}|}
+\hline
+\multicolumn{1}{|l}{Name} & \multicolumn{3}{l|}{\bf event} \\
+\multicolumn{1}{|l}{Description} & \multicolumn{3}{l|}{\parbox{11cm}{\em
+Asynchronous event registration and handling.}} \\
+\hline
+Quals & Field & Type & Description \\
+\hline
+$\mathit{RO}_\mathit{ins}$ &  {\tt id} & int & An ID, monotonically increasing, and local to the current session \\
+$\mathit{RO}_\mathit{ins}$ &  {\tt timestamp} & datetime & The time at which the event occurred \\
+$\mathit{RO}_\mathit{ins}$ &  {\tt class} & string & The name of the class of the object that changed \\
+$\mathit{RO}_\mathit{ins}$ &  {\tt operation} & event\_operation & The operation that was performed \\
+$\mathit{RO}_\mathit{ins}$ &  {\tt ref} & string & A reference to the object that changed \\
+$\mathit{RO}_\mathit{ins}$ &  {\tt obj\_uuid} & string & The uuid of the object that changed \\
+\hline
+\end{longtable}
+\subsection{RPCs associated with class: event}
+\subsubsection{RPC name:~register}
+
+{\bf Overview:} 
+Registers this session with the event system.  Specifying the empty list
+will register for all classes.
+
+ \noindent {\bf Signature:} 
+\begin{verbatim} void register (session_id s, string Set classes)\end{verbatim}
+
+
+\noindent{\bf Arguments:}
+
+\vspace{0.3cm}
+\begin{tabular}{|c|c|p{7cm}|}
+ \hline
+{\bf type} & {\bf name} & {\bf description} \\ \hline
+{\tt string Set } & classes & register for events for the indicated classes \\ \hline 
+
+\end{tabular}
+
+\vspace{0.3cm}
+
+ \noindent {\bf Return Type:} 
+{\tt 
+void
+}
+
+
+
+\vspace{0.3cm}
+\vspace{0.3cm}
+\vspace{0.3cm}
+\subsubsection{RPC name:~unregister}
+
+{\bf Overview:} 
+Unregisters this session with the event system.
+
+ \noindent {\bf Signature:} 
+\begin{verbatim} void unregister (session_id s, string Set classes)\end{verbatim}
+
+
+\noindent{\bf Arguments:}
+
+\vspace{0.3cm}
+\begin{tabular}{|c|c|p{7cm}|}
+ \hline
+{\bf type} & {\bf name} & {\bf description} \\ \hline
+{\tt string Set } & classes & remove this session's registration for the indicated classes \\ \hline 
+
+\end{tabular}
+
+\vspace{0.3cm}
+
+ \noindent {\bf Return Type:} 
+{\tt 
+void
+}
+
+
+
+\vspace{0.3cm}
+\vspace{0.3cm}
+\vspace{0.3cm}
+\subsubsection{RPC name:~next}
+
+{\bf Overview:} 
+Blocking call which returns a (possibly empty) batch of events.
+
+ \noindent {\bf Signature:} 
+\begin{verbatim} ((event record) Set) next (session_id s)\end{verbatim}
+
+
+\vspace{0.3cm}
+
+ \noindent {\bf Return Type:} 
+{\tt 
+(event record) Set
+}
+
+
+the batch of events
+\vspace{0.3cm}
+\vspace{0.3cm}
+\vspace{0.3cm}
+
 \vspace{1cm}
 \newpage
 \section{Class: VM}
index 58db515a72354c6afd9a173843cf6df669db51ec..51062d7c5b2a4ab0bab235a65e6cbf4893aa30e1 100644 (file)
@@ -51,6 +51,9 @@ libxenapi.a: $(LIBXENAPI_OBJS)
 test/test_bindings: test/test_bindings.o libxenapi.so
        $(CC) $(LDFLAGS) -o $@ $< -L . -lxenapi
 
+test/test_event_handling: test/test_event_handling.o libxenapi.so
+       $(CC) $(LDFLAGS) -o $@ $< -L . -lxenapi
+
 test/test_hvm_bindings: test/test_hvm_bindings.o libxenapi.so
        $(CC) $(LDFLAGS) -o $@ $< -L . -lxenapi
 
diff --git a/tools/libxen/include/xen_event.h b/tools/libxen/include/xen_event.h
new file mode 100644 (file)
index 0000000..1e171b8
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2006-2007, XenSource Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ */
+
+#ifndef XEN_EVENT_H
+#define XEN_EVENT_H
+
+#include "xen_common.h"
+#include "xen_event_decl.h"
+#include "xen_event_operation.h"
+#include "xen_string_set.h"
+
+
+/*
+ * The event class.
+ * 
+ * Asynchronous event registration and handling.
+ */
+
+
+
+typedef struct xen_event_record
+{
+    int64_t id;
+    time_t timestamp;
+    char *class;
+    enum xen_event_operation operation;
+    char *ref;
+    char *obj_uuid;
+} xen_event_record;
+
+/**
+ * Allocate a xen_event_record.
+ */
+extern xen_event_record *
+xen_event_record_alloc(void);
+
+/**
+ * Free the given xen_event_record, and all referenced values.  The
+ * given record must have been allocated by this library.
+ */
+extern void
+xen_event_record_free(xen_event_record *record);
+
+
+typedef struct xen_event_record_set
+{
+    size_t size;
+    xen_event_record *contents[];
+} xen_event_record_set;
+
+/**
+ * Allocate a xen_event_record_set of the given size.
+ */
+extern xen_event_record_set *
+xen_event_record_set_alloc(size_t size);
+
+/**
+ * Free the given xen_event_record_set, and all referenced values.  The
+ * given set must have been allocated by this library.
+ */
+extern void
+xen_event_record_set_free(xen_event_record_set *set);
+
+
+/**
+ * Registers this session with the event system.  Specifying the empty
+ * list will register for all classes.
+ */
+extern bool
+xen_event_register(xen_session *session, struct xen_string_set *classes);
+
+
+/**
+ * Unregisters this session with the event system.
+ */
+extern bool
+xen_event_unregister(xen_session *session, struct xen_string_set *classes);
+
+
+/**
+ * Blocking call which returns a (possibly empty) batch of events.
+ */
+extern bool
+xen_event_next(xen_session *session, struct xen_event_record_set **result);
+
+
+#endif
diff --git a/tools/libxen/include/xen_event_decl.h b/tools/libxen/include/xen_event_decl.h
new file mode 100644 (file)
index 0000000..856991f
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2006-2007, XenSource Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ */
+
+#ifndef XEN_EVENT_DECL_H
+#define XEN_EVENT_DECL_H
+
+struct xen_event_record;
+struct xen_event_record_set;
+
+#endif
diff --git a/tools/libxen/include/xen_event_operation.h b/tools/libxen/include/xen_event_operation.h
new file mode 100644 (file)
index 0000000..05319ef
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2006-2007, XenSource Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ */
+
+#ifndef XEN_EVENT_OPERATION_H
+#define XEN_EVENT_OPERATION_H
+
+
+#include "xen_common.h"
+
+
+enum xen_event_operation
+{
+    /**
+     * An object has been created
+     */
+    XEN_EVENT_OPERATION_ADD,
+
+    /**
+     * An object has been deleted
+     */
+    XEN_EVENT_OPERATION_DEL,
+
+    /**
+     * An object has been modified
+     */
+    XEN_EVENT_OPERATION_MOD
+};
+
+
+typedef struct xen_event_operation_set
+{
+    size_t size;
+    enum xen_event_operation contents[];
+} xen_event_operation_set;
+
+/**
+ * Allocate a xen_event_operation_set of the given size.
+ */
+extern xen_event_operation_set *
+xen_event_operation_set_alloc(size_t size);
+
+/**
+ * Free the given xen_event_operation_set.  The given set must have
+ * been allocated by this library.
+ */
+extern void
+xen_event_operation_set_free(xen_event_operation_set *set);
+
+
+/**
+ * Return the name corresponding to the given code.  This string must
+ * not be modified or freed.
+ */
+extern const char *
+xen_event_operation_to_string(enum xen_event_operation val);
+
+
+/**
+ * Return the correct code for the given string, or set the session
+ * object to failure and return an undefined value if the given string does
+ * not match a known code.
+ */
+extern enum xen_event_operation
+xen_event_operation_from_string(xen_session *session, const char *str);
+
+
+#endif
diff --git a/tools/libxen/include/xen_event_operation_internal.h b/tools/libxen/include/xen_event_operation_internal.h
new file mode 100644 (file)
index 0000000..3c4f70c
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2006-2007, XenSource Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ */
+
+
+/*
+ * Declarations of the abstract types used during demarshalling of enum
+ * xen_event_operation.  Internal to this library -- do not use from outside.
+ */
+
+
+#ifndef XEN_EVENT_OPERATION_INTERNAL_H
+#define XEN_EVENT_OPERATION_INTERNAL_H
+
+
+#include "xen_internal.h"
+
+
+extern const abstract_type xen_event_operation_abstract_type_;
+extern const abstract_type xen_event_operation_set_abstract_type_;
+
+
+#endif
diff --git a/tools/libxen/src/xen_event.c b/tools/libxen/src/xen_event.c
new file mode 100644 (file)
index 0000000..3022caf
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2006-2007, XenSource Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ */
+
+
+#include <stddef.h>
+#include <stdlib.h>
+
+#include "xen_common.h"
+#include "xen_event.h"
+#include "xen_event_operation_internal.h"
+#include "xen_internal.h"
+
+
+XEN_ALLOC(xen_event_record)
+XEN_SET_ALLOC_FREE(xen_event_record)
+
+
+static const struct_member xen_event_record_struct_members[] =
+    {
+        { .key = "id",
+          .type = &abstract_type_int,
+          .offset = offsetof(xen_event_record, id) },
+        { .key = "timestamp",
+          .type = &abstract_type_datetime,
+          .offset = offsetof(xen_event_record, timestamp) },
+        { .key = "class",
+          .type = &abstract_type_string,
+          .offset = offsetof(xen_event_record, class) },
+        { .key = "operation",
+          .type = &xen_event_operation_abstract_type_,
+          .offset = offsetof(xen_event_record, operation) },
+        { .key = "ref",
+          .type = &abstract_type_string,
+          .offset = offsetof(xen_event_record, ref) },
+        { .key = "obj_uuid",
+          .type = &abstract_type_string,
+          .offset = offsetof(xen_event_record, obj_uuid) }
+    };
+
+const abstract_type xen_event_record_abstract_type_ =
+    {
+       .typename = STRUCT,
+       .struct_size = sizeof(xen_event_record),
+       .member_count =
+           sizeof(xen_event_record_struct_members) / sizeof(struct_member),
+       .members = xen_event_record_struct_members
+    };
+
+
+const abstract_type xen_event_record_set_abstract_type_ =
+    {
+       .typename = SET,
+        .child = &xen_event_record_abstract_type_
+    };
+
+
+void
+xen_event_record_free(xen_event_record *record)
+{
+    if (record == NULL)
+    {
+        return;
+    }
+    free(record->class);
+    free(record->ref);
+    free(record->obj_uuid);
+    free(record);
+}
+
+
+bool
+xen_event_register(xen_session *session, struct xen_string_set *classes)
+{
+    abstract_value param_values[] =
+        {
+            { .type = &abstract_type_string_set,
+              .u.set_val = (arbitrary_set *)classes }
+        };
+
+    xen_call_(session, "event.register", param_values, 1, NULL, NULL);
+    return session->ok;
+}
+
+
+bool
+xen_event_unregister(xen_session *session, struct xen_string_set *classes)
+{
+    abstract_value param_values[] =
+        {
+            { .type = &abstract_type_string_set,
+              .u.set_val = (arbitrary_set *)classes }
+        };
+
+    xen_call_(session, "event.unregister", param_values, 1, NULL, NULL);
+    return session->ok;
+}
+
+
+bool
+xen_event_next(xen_session *session, struct xen_event_record_set **result)
+{
+
+    abstract_type result_type = xen_event_record_set_abstract_type_;
+
+    *result = NULL;
+    xen_call_(session, "event.next", NULL, 0, &result_type, result);
+    return session->ok;
+}
diff --git a/tools/libxen/src/xen_event_operation.c b/tools/libxen/src/xen_event_operation.c
new file mode 100644 (file)
index 0000000..c338af0
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2006-2007, XenSource Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ */
+
+#include <string.h>
+
+#include "xen_internal.h"
+#include "xen_event_operation.h"
+#include "xen_event_operation_internal.h"
+
+
+/*
+ * Maintain this in the same order as the enum declaration!
+ */
+static const char *lookup_table[] =
+{
+    "add",
+    "del",
+    "mod"
+};
+
+
+extern xen_event_operation_set *
+xen_event_operation_set_alloc(size_t size)
+{
+    return calloc(1, sizeof(xen_event_operation_set) +
+                  size * sizeof(enum xen_event_operation));
+}
+
+
+extern void
+xen_event_operation_set_free(xen_event_operation_set *set)
+{
+    free(set);
+}
+
+
+const char *
+xen_event_operation_to_string(enum xen_event_operation val)
+{
+    return lookup_table[val];
+}
+
+
+extern enum xen_event_operation
+xen_event_operation_from_string(xen_session *session, const char *str)
+{
+    return ENUM_LOOKUP(session, str, lookup_table);
+}
+
+
+const abstract_type xen_event_operation_abstract_type_ =
+    {
+        .typename = ENUM,
+        .enum_marshaller =
+             (const char *(*)(int))&xen_event_operation_to_string,
+        .enum_demarshaller =
+             (int (*)(xen_session *, const char *))&xen_event_operation_from_string
+    };
+
+
diff --git a/tools/libxen/test/test_event_handling.c b/tools/libxen/test/test_event_handling.c
new file mode 100644 (file)
index 0000000..5263894
--- /dev/null
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2006-2007 XenSource, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ */
+
+#define _GNU_SOURCE
+#include <assert.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <libxml/parser.h>
+#include <curl/curl.h>
+
+#include "xen_event.h"
+
+//#define PRINT_XML
+
+static void usage()
+{
+    fprintf(stderr,
+"Usage:\n"
+"\n"
+"    test_event_handling <server> <username> <password>\n"
+"\n"
+"where\n"
+"        <server>   is the server's host and port, e.g. localhost:9363;\n"
+"        <username> is the username to use at the server; and\n"
+"        <password> is the password.\n");
+
+    exit(EXIT_FAILURE);
+}
+
+
+static char *url;
+
+
+typedef struct
+{
+    xen_result_func func;
+    void *handle;
+} xen_comms;
+
+
+static size_t
+write_func(void *ptr, size_t size, size_t nmemb, xen_comms *comms)
+{
+    size_t n = size * nmemb;
+#ifdef PRINT_XML
+    printf("\n\n---Result from server -----------------------\n");
+    printf("%s\n",((char*) ptr));
+    fflush(stdout);
+#endif
+    return comms->func(ptr, n, comms->handle) ? n : 0;
+}
+
+
+static int
+call_func(const void *data, size_t len, void *user_handle,
+          void *result_handle, xen_result_func result_func)
+{
+    (void)user_handle;
+
+#ifdef PRINT_XML
+    printf("\n\n---Data to server: -----------------------\n");
+    printf("%s\n",((char*) data));
+    fflush(stdout);
+#endif
+
+    CURL *curl = curl_easy_init();
+    if (!curl) {
+        return -1;
+    }
+
+    xen_comms comms = {
+        .func = result_func,
+        .handle = result_handle
+    };
+
+    curl_easy_setopt(curl, CURLOPT_URL, url);
+    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
+    curl_easy_setopt(curl, CURLOPT_MUTE, 1);
+    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_func);
+    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &comms);
+    curl_easy_setopt(curl, CURLOPT_POST, 1);
+    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
+    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len);
+
+    CURLcode result = curl_easy_perform(curl);
+
+    curl_easy_cleanup(curl);
+
+    return result;
+}
+
+
+static void print_error(xen_session *session)
+{
+    fprintf(stderr, "Error: %d", session->error_description_count);
+    for (int i = 0; i < session->error_description_count; i++)
+    {
+        fprintf(stderr, "%s ", session->error_description[i]);
+    }
+    fprintf(stderr, "\n");
+}
+
+
+/**
+ * Workaround for whinging GCCs, as suggested by strftime(3).
+ */
+static size_t my_strftime(char *s, size_t max, const char *fmt,
+                          const struct tm *tm)
+{
+    return strftime(s, max, fmt, tm);
+}
+
+
+int main(int argc, char **argv)
+{
+    if (argc != 4)
+    {
+        usage();
+    }
+
+    url = argv[1];
+    char *username = argv[2];
+    char *password = argv[3];
+
+    xmlInitParser();
+    xen_init();
+    curl_global_init(CURL_GLOBAL_ALL);
+
+#define CLEANUP                                 \
+    do {                                        \
+        xen_session_logout(session);            \
+        curl_global_cleanup();                  \
+        xen_fini();                             \
+        xmlCleanupParser();                     \
+    } while(0)                                  \
+
+    
+    xen_session *session =
+        xen_session_login_with_password(call_func, NULL, username, password);
+
+    struct xen_string_set *classes = xen_string_set_alloc(0);
+    xen_event_register(session, classes);
+    xen_string_set_free(classes);
+
+    if (!session->ok)
+    {
+        print_error(session);
+        CLEANUP;
+        return 1;
+    }
+
+    while (true)
+    {
+        struct xen_event_record_set *events;
+        if (!xen_event_next(session, &events))
+        {
+            print_error(session);
+            CLEANUP;
+            return 1;
+        }
+
+        for (size_t i = 0; i < events->size; i++)
+        {
+            xen_event_record *ev = events->contents[i];
+            char time[256];
+            struct tm *tm = localtime(&ev->timestamp);
+            my_strftime(time, 256, "%c, local time", tm);
+            printf("Event received: ID = %"PRId64", %s.\n", ev->id, time);
+            switch (ev->operation)
+            {
+            case XEN_EVENT_OPERATIONS_ADD:
+                printf("%s created with UUID %s.\n", ev->class, ev->obj_uuid);
+                break;
+
+            case XEN_EVENT_OPERATIONS_DEL:
+                printf("%s with UUID %s deleted.\n", ev->class, ev->obj_uuid);
+                break;
+
+            case XEN_EVENT_OPERATIONS_MOD:
+                printf("%s with UUID %s modified.\n", ev->class, ev->obj_uuid);
+                break;
+            default:
+                assert(false);
+            }
+        }
+
+        xen_event_record_set_free(events);
+    }
+
+    CLEANUP;
+
+    return 0;
+}
index 2617287165c045df433f93b880f65126bd3321b3..af175183b63efd5cdeb1364910c697a84a3ceef3 100644 (file)
@@ -17,6 +17,8 @@
 
 import inspect
 import os
+import Queue
+import sets
 import string
 import sys
 import traceback
@@ -86,6 +88,91 @@ def now():
     return xmlrpclib.DateTime(time.strftime("%Y%m%dT%H:%M:%S", time.gmtime()))
 
 
+# ---------------------------------------------------
+# Event dispatch
+# ---------------------------------------------------
+
+EVENT_QUEUE_LENGTH = 50
+event_registrations = {}
+
+def event_register(session, reg_classes):
+    if session not in event_registrations:
+        event_registrations[session] = {
+            'classes' : sets.Set(),
+            'queue'   : Queue.Queue(EVENT_QUEUE_LENGTH),
+            'next-id' : 1
+            }
+    if not reg_classes:
+        reg_classes = classes
+    event_registrations[session]['classes'].union_update(reg_classes)
+
+
+def event_unregister(session, unreg_classes):
+    if session not in event_registrations:
+        return
+
+    if unreg_classes:
+        event_registrations[session]['classes'].intersection_update(
+            unreg_classes)
+        if len(event_registrations[session]['classes']) == 0:
+            del event_registrations[session]
+    else:
+        del event_registrations[session]
+
+
+def event_next(session):
+    if session not in event_registrations:
+        return xen_api_error(['SESSION_INVALID', session])
+    queue = event_registrations[session]['queue']
+    events = [queue.get()]
+    try:
+        while True:
+            events.append(queue.get(False))
+    except Queue.Empty:
+        pass
+
+    return xen_api_success(events)
+
+
+def _ctor_event_dispatch(xenapi, ctor, api_cls, session, args):
+    result = ctor(xenapi, session, *args)
+    if result['Status'] == 'Success':
+        ref = result['Value']
+        _event_dispatch('add', api_cls, ref, '')
+    return result
+
+
+def _dtor_event_dispatch(xenapi, dtor, api_cls, session, ref, args):
+    result = dtor(xenapi, session, ref, *args)
+    if result['Status'] == 'Success':
+        _event_dispatch('del', api_cls, ref, '')
+    return result
+
+
+def _setter_event_dispatch(xenapi, setter, api_cls, attr_name, session, ref,
+                           args):
+    result = setter(xenapi, session, ref, *args)
+    if result['Status'] == 'Success':
+        _event_dispatch('mod', api_cls, ref, attr_name)
+    return result
+
+
+def _event_dispatch(operation, api_cls, ref, attr_name):
+    event = {
+        'timestamp' : now(),
+        'class'     : api_cls,
+        'operation' : operation,
+        'ref'       : ref,
+        'obj_uuid'  : ref,
+        'field'     : attr_name,
+        }
+    for reg in event_registrations.values():
+        if api_cls in reg['classes']:
+            event['id'] = reg['next-id']
+            reg['next-id'] += 1
+            reg['queue'].put(event)
+
+
 # ---------------------------------------------------
 # Python Method Decorators for input value validation
 # ---------------------------------------------------
@@ -375,6 +462,36 @@ def do_vm_func(fn_name, vm_ref, *args, **kwargs):
                               exn.actual])
 
 
+classes = {
+    'session'      : None,
+    'event'        : None,
+    'host'         : valid_host,
+    'host_cpu'     : valid_host_cpu,
+    'host_metrics' : valid_host_metrics,
+    'network'      : valid_network,
+    'VM'           : valid_vm,
+    'VM_metrics'   : valid_vm_metrics,
+    'VBD'          : valid_vbd,
+    'VBD_metrics'  : valid_vbd_metrics,
+    'VIF'          : valid_vif,
+    'VIF_metrics'  : valid_vif_metrics,
+    'VDI'          : valid_vdi,
+    'VTPM'         : valid_vtpm,
+    'console'      : valid_console,
+    'SR'           : valid_sr,
+    'PIF'          : valid_pif,
+    'PIF_metrics'  : valid_pif_metrics,
+    'task'         : valid_task,
+    'debug'        : valid_debug,
+}
+
+autoplug_classes = {
+    'network'     : XendNetwork,
+    'VM_metrics'  : XendVMMetrics,
+    'PIF_metrics' : XendPIFMetrics,
+}
+
+
 class XendAPI(object):
     """Implementation of the Xen-API in Xend. Expects to be
     used via XMLRPCServer.
@@ -416,33 +533,7 @@ class XendAPI(object):
         server.
         """
         global_validators = [session_required, catch_typeerror]
-        classes = {
-            'session'      : None,
-            'host'         : valid_host,
-            'host_cpu'     : valid_host_cpu,
-            'host_metrics' : valid_host_metrics,
-            'network'      : valid_network,
-            'VM'           : valid_vm,
-            'VM_metrics'   : valid_vm_metrics,
-            'VBD'          : valid_vbd,
-            'VBD_metrics'  : valid_vbd_metrics,
-            'VIF'          : valid_vif,
-            'VIF_metrics'  : valid_vif_metrics,
-            'VDI'          : valid_vdi,
-            'VTPM'         : valid_vtpm,
-            'console'      : valid_console,
-            'SR'           : valid_sr,
-            'PIF'          : valid_pif,
-            'PIF_metrics'  : valid_pif_metrics,
-            'task'         : valid_task,
-            'debug'        : valid_debug,
-        }
 
-        autoplug_classes = {
-            'network'     : XendNetwork,
-            'VM_metrics'  : XendVMMetrics,
-            'PIF_metrics' : XendPIFMetrics,
-        }
 
         # Cheat methods
         # -------------
@@ -499,6 +590,43 @@ class XendAPI(object):
                 doit('%s' % func_name)
 
 
+        def wrap_method(name, new_f):
+            try:
+                f = getattr(cls, name)
+                wrapped_f = (lambda *args: new_f(f, *args))
+                wrapped_f.api = f.api
+                wrapped_f.async = f.async
+                setattr(cls, name, wrapped_f)
+            except AttributeError:
+                # Logged below (API call: %s not found)
+                pass
+
+
+        def setter_event_wrapper(api_cls, attr_name):
+            setter_name = '%s_set_%s' % (api_cls, attr_name)
+            wrap_method(
+                setter_name,
+                lambda setter, s, session, ref, *args:
+                _setter_event_dispatch(s, setter, api_cls, attr_name,
+                                       session, ref, args))
+
+
+        def ctor_event_wrapper(api_cls):
+            ctor_name = '%s_create' % api_cls
+            wrap_method(
+                ctor_name,
+                lambda ctor, s, session, *args:
+                _ctor_event_dispatch(s, ctor, api_cls, session, args))
+
+
+        def dtor_event_wrapper(api_cls):
+            dtor_name = '%s_destroy' % api_cls
+            wrap_method(
+                dtor_name,
+                lambda dtor, s, session, ref, *args:
+                _dtor_event_dispatch(s, dtor, api_cls, session, ref, args))
+
+
         # Wrapping validators around XMLRPC calls
         # ---------------------------------------
 
@@ -541,6 +669,7 @@ class XendAPI(object):
             for attr_name in rw_attrs + cls.Base_attr_rw:
                 doit('%s.set_%s' % (api_cls, attr_name), True,
                      async_support = False)
+                setter_event_wrapper(api_cls, attr_name)
 
             # wrap validators around methods
             for method_name, return_type in methods + cls.Base_methods:
@@ -552,6 +681,10 @@ class XendAPI(object):
                 doit('%s.%s' % (api_cls, func_name), False, async_support = True,
                      return_type = return_type)
 
+            ctor_event_wrapper(api_cls)
+            dtor_event_wrapper(api_cls)
+
+
     _decorate = classmethod(_decorate)
 
     def __init__(self, auth):
@@ -2388,6 +2521,26 @@ class XendAPI(object):
         return xen_api_success_void()
 
 
+    # Xen API: Class event
+    # ----------------------------------------------------------------
+
+    event_attr_ro = []
+    event_attr_rw = []
+    event_funcs = [('register', None),
+                   ('unregister', None),
+                   ('next', None)]
+
+    def event_register(self, session, reg_classes):
+        event_register(session, reg_classes)
+        return xen_api_success_void()
+
+    def event_unregister(self, session, unreg_classes):
+        event_unregister(session, reg_classes)
+        return xen_api_success_void()
+
+    def event_next(self, session):
+        return event_next(session)
+
     # Xen API: Class debug
     # ----------------------------------------------------------------
 
index 77d647f4026f4c1035767795d792715bcd834c42..35d6305b36b3f35f3e46f7c72c5c6cd3df0c11eb 100644 (file)
@@ -345,7 +345,8 @@ acm_commands = [
     ]
 
 all_commands = (domain_commands + host_commands + scheduler_commands +
-                device_commands + vnet_commands + acm_commands + ['shell'])
+                device_commands + vnet_commands + acm_commands +
+                ['shell', 'event-monitor'])
 
 
 ##
@@ -633,6 +634,17 @@ def xm_shell(args):
     Shell().cmdloop('The Xen Master. Type "help" for a list of functions.')
 
 
+def xm_event_monitor(args):
+    if serverType == SERVER_XEN_API:
+        while True:
+            server.xenapi.event.register(args)
+            events = server.xenapi.event.next()
+            for e in events:
+                print e
+    else:
+        err("Event monitoring not supported unless using Xen-API.")
+
+
 #########################################################################
 #
 #  Main xm functions
@@ -2169,6 +2181,7 @@ def xm_vnet_delete(args):
 
 commands = {
     "shell": xm_shell,
+    "event-monitor": xm_event_monitor,
     # console commands
     "console": xm_console,
     # xenstat commands